package com.practice.service;

import com.github.pagehelper.PageHelper;
import com.practice.dto.RequestDto3;
import com.practice.entity.Account;
import com.practice.mapper.AccUserMapper;
import com.practice.vo.AccUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by IntelliJ IDEA2021.3
 * @Author: Tenghw
 * @Date: 2022/12/10  23:41
 * @Description:
 */
@Slf4j
@Service
public class AccUserService {

    @Autowired
    AccUserMapper accUserMapper;

    //public List<AccUserVo> queryByAny(RequestDto2 requestDto2){
    //    log.info("1当前页{}，每页多少条{}",requestDto2.getCurrPage(),requestDto2.getPageSize());
    //    PageHelper.startPage(requestDto2.getCurrPage(), requestDto2.getPageSize());
    //    //PageHelper.offsetPage((requestDto2.getCurrPage()-1)*requestDto2.getPageSize(), requestDto2.getPageSize());//和上一行代码等效
    //    List<AccUserVo> accUserVoList = accUserMapper.queryByAny(requestDto2);
    //    return accUserVoList;
    //}

    //public List<AccUserVo> queryByAny(RequestDto2 requestDto2){
    //    log.info("当前页{}，每页多少条{}",requestDto2.getCurrPage(),requestDto2.getPageSize());
    //    PageHelper.startPage(requestDto2.getCurrPage(), requestDto2.getPageSize());
    //    List<AccUserVo> accUserVoList = accUserMapper.queryByAny(requestDto2);
    //    return accUserVoList;
    //}

    //public PageInfo<AccUserVo> queryByAny(RequestDto2 requestDto2){
    //    log.info("当前页{}，每页多少条{}",requestDto2.getCurrPage(),requestDto2.getPageSize());
    //    PageHelper.startPage(requestDto2.getCurrPage(), requestDto2.getPageSize());
    //    List<AccUserVo> accUserVoList = accUserMapper.queryByAny(requestDto2);
    //    PageInfo<AccUserVo> pageInfo = new PageInfo<>(accUserVoList);
    //    return pageInfo;
    //}

    //public PageInfo<AccUserVo> queryByAny(RequestDto3 requestDto3){
    //    log.info("当前页{}，每页多少条{}",requestDto3.getCurrPage(),requestDto3.getPageSize());
    //    //分页页码初始化
    //    requestDto3.init();
    //    //还是要加下面这一行代码，不然执行的sql语句不带limt，即查询所有数据而不自动分页
    //    PageHelper.startPage(requestDto3.getCurrPage(),requestDto3.getPageSize());
    //    List<AccUserVo> accUserVoList = accUserMapper.queryByAny(requestDto3);
    //    PageInfo<AccUserVo> pageInfo = new PageInfo<>(accUserVoList);
    //    PageInfo<AccUserVo> pageInfo1= PageHelperTool.initPageInfoObj(requestDto3.getCurrPage(), (int) pageInfo.getTotal(), requestDto3.getPageSize(), pageInfo);
    //    return pageInfo1;
    //}

    public List<AccUserVo> queryByAny(RequestDto3 requestDto3){
        log.info("当前页{}，每页多少条{}",requestDto3.getCurrPage(),requestDto3.getPageSize());
        //分页页码初始化
        requestDto3.init();
        //还是要加下面这一行代码，不然执行的sql语句不带limt，即查询所有数据而不自动分页
        PageHelper.startPage(requestDto3.getCurrPage(),requestDto3.getPageSize());
        List<AccUserVo> accUserVoList = accUserMapper.queryByAny(requestDto3);
        return accUserVoList;
    }
    @Transactional(rollbackFor = Exception.class)
    public void addAccount(Account account){
        int addAccount = accUserMapper.addAccount(account);
        if(addAccount != 1){
            log.error("添加账户失败");
            throw new RuntimeException("添加账户失败");
        }
    }

}
